//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
// 			VisualNovelToolkit		     /_/_/_/_/_/_/_/_/_/.
// Copyright ©2013 - Sol-tribe.	/_/_/_/_/_/_/_/_/_/.
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/.
using UnityEngine;
using System.Collections;

namespace ViNoToolkit{

/// <summary>
/// Simple button with Tween.
/// </summary>
[AddComponentMenu( "ViNo/UI/Button" )]
public class SimpleButton : ViNoToolkit.ButtonBase{

	public override void OnAwake(){
		if (clickSound != null) {
			clickSoundAudioSource = gameObject.AddComponent<AudioSource>();
			clickSoundAudioSource.clip = clickSound;
		}
	}
	
	public override void SetNormalColor( float duration ){
		iTween.ColorTo (this.gameObject, normalColor, duration );
	}

	public override void SetHoverColor( float duration ){
		iTween.ColorTo (this.gameObject, focusColor, duration );	
	}

	/// <summary>
	/// Raises the touch began event.
	/// </summary>
	public override bool OnTouchBegan(){
		if( TouchScreen.selectedGameObject == this.gameObject ){
			ViNoDebugger.Log( "INPUT" , "OnTouchBegan :" + name );
			m_TouchObjectResponse = touchResponse;			
			switch( touchResponse ){
				case TouchResponse.OFFSET:
					m_TouchedObject = this.gameObject;
					m_TouchBeganAmount = m_TouchedObject.transform.localPosition;
					Vector3 pos = m_TouchBeganAmount + offsetAmount;					
					if( animate ){
						iTween.MoveTo( this.gameObject, iTween.Hash("position", pos, "islocal", true, "time", 1));	
					}
					else{
						m_TouchedObject.transform.localPosition = pos;
					}
					break;

				case TouchResponse.SCALE:
					m_TouchedObject = this.gameObject;
					m_TouchBeganAmount = Vector3.one;
					Vector3 scl = new Vector3( scaleAmount.x ,scaleAmount.y , scaleAmount.z );
					if( animate ){
						iTween.ScaleTo( m_TouchedObject , scl , 1f );
					}
					else{
						m_TouchedObject.transform.localScale = new Vector3( m_TouchBeganAmount.x * scaleAmount.x ,m_TouchBeganAmount.y * scaleAmount.y , scaleAmount.z );
					}
					break;
				case TouchResponse.NONE:
					break;
			}
			return true;
		}
		else{
			return false;
		}
	}
	
	/// <summary>
	/// Raises the touch out event.
	/// </summary>
	public override bool OnTouchOut(){
		if( m_TouchedObject != null ){
			ViNoDebugger.Log( "INPUT" , "OnTouchOut:" + name );
			ResetBeganAmount( m_TouchedObject.transform );
			m_TouchedObject = null;
		}
		return true;
	}

	/// <summary>
	/// Raises the touch ended event.
	/// </summary>
	public override bool OnTouchEnded(){		
		if( m_TouchedObject != null ){
			ResetBeganAmount( m_TouchedObject.transform );
			m_TouchedObject = null;
		}

		if( TouchScreen.selectedGameObject == this.gameObject ){
			ViNoDebugger.Log( "INPUT" , "OnTouchEnded :" + name );						
			if (clickSoundAudioSource != null) {
				clickSoundAudioSource.PlayDelayed( 0.1f );
			}
			
			if( findObjectAndSendMessage ){
				eventReceiver = GameObject.Find( targetObjectName );
				if( eventReceiver == null ){
					Debug.Log( "Target :" + targetObjectName + " not found." );
				}
			}
			if( eventReceiver != null ){
				if( ! string.IsNullOrEmpty( paramString )){
					eventReceiver.SendMessage( sendMessage , paramString , SendMessageOptions.DontRequireReceiver );
				}
				else{
					eventReceiver.SendMessage( sendMessage , SendMessageOptions.DontRequireReceiver );
				}
			}						
			return true;
		}
		else{
			return false;
		}
	}

}

}